package com.example.jpatest.webapi;

import com.example.jpatest.entity.Student;
import com.example.jpatest.service.StudentService;
import com.example.jpatest.utils.RUtil;
import com.example.jpatest.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginApiController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/user")
    public int student(HttpServletRequest httpServletRequest, String username, String password) throws Exception {
        List<Student> students = studentService.findByNameAndPassword(username, password);
        if (students.size() > 0) {
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("userid", students.get(0).getId());
            session.setAttribute("user", students.get(0));
            return 1;
        } else {
            return 0;
        }
    }
}
